from flask import Blueprint,render_template,g,redirect,request
from app import db
from app.models import Product,User,Acctproduct,Updateturn,Updatejour
import os
import time

histurn_page = Blueprint('histurn_page',__name__,template_folder='templates')
@histurn_page.route('/histurn',methods = ['GET','POST'])
def histurn():
    if g.user is not None and g.user.is_authenticated:
        hs_current_id = str(g.user).replace("'", "")
        hs_current_user = db.session.query(User.username).filter_by(id=hs_current_id).first()[0]
        mes_lists = db.session.query(Updatejour.output,Updatejour.update_date,Updatejour.update_time,Updatejour.message,Updatejour.id,Updatejour.log_path).filter_by(read_status = 1,up_persion=hs_current_user).all()
        mes_len = len(mes_lists)
        histurn_lists = []
        if request.method == 'POST':
            if request.values.get('fun')=='hisquery':
                range = request.values.get('daterange')
                a = range.split(' ')
                begin1 = a[0].split('/')
                Begin_date = int(begin1[2] + begin1[0] + begin1[1])
                end1 = a[2].split('/')
                End_date = int(end1[2] + end1[0] + end1[1])
                histurn_lists = db.session.query(Updatejour.output,Updatejour.read_status,
                                                 Updatejour.message, Updatejour.update_date,
                                                 Updatejour.up_persion).filter(Updatejour.update_date.between(Begin_date,End_date)).all()
    else:
        return redirect('/login')
    return render_template('hs-histurn.html',hs_current_user=hs_current_user,histurn_lists=histurn_lists,mes_lists=mes_lists,mes_len=mes_len)